package de.hska.algo.labor2010.strecken;

import static org.junit.Assert.*;

import java.util.ArrayList;

import junit.framework.Assert;

import org.junit.Test;

public class GridPartTest {

	@Test
	public void testGetUpperLeft() {
		GridPart part = new GridPart(0, 0, 10, 10);
		Point lowerLeft = part.getLowerLeft();
		assertEquals(0.0, lowerLeft.getX(), 1.9e-16);
		assertEquals(0.0, lowerLeft.getY(), 1.9e-16);
	}

	
	@Test
	public void testGetLowerRight() {
		GridPart part = new GridPart(0, 0, 10, 10);
		Point upperRight = part.getUpperRight();
		assertEquals(10.0, upperRight.getX(), 1.9e-16);
		assertEquals(10.0, upperRight.getY(), 1.9e-16);
	}

	@Test
	public void testGetThisLineisInside() {
		GridPart part = new GridPart(0, 0, 10, 10);
		part.setThisLineisInside(true);
		assertTrue(part.getThisLineisInside());
		part.setThisLineisInside(false);
		assertFalse(part.getThisLineisInside());

	}

	@Test
	public void testSetThisLineisInside() {
		GridPart part = new GridPart(0, 0, 10, 10);
		part.setThisLineisInside(true);
		assertTrue(part.getThisLineisInside());
		part.setThisLineisInside(false);
		assertFalse(part.getThisLineisInside());
	}

	@Test
	public void testGetLines() {
		GridPart part = new GridPart(0, 0, 10, 10);
		Line line = new Line(0.0, 0.0, 10.0, 10.0);
		part.addLine(line);
		assertEquals(0.0, part.getLines().get(0).getP1().getX(), 1.9e-16);
		assertEquals(0.0, part.getLines().get(0).getP1().getY(), 1.9e-16);
		assertEquals(10.0, part.getLines().get(0).getP2().getX(), 1.9e-16);
		assertEquals(10.0, part.getLines().get(0).getP2().getY(), 1.9e-16);
	}

	@Test
	public void testGridPart() {
		GridPart part = new GridPart(0, 0, 10, 10);

		assertEquals(0.0, part.getLowerLeft().getX(), 1.9e-16);
		assertEquals(0.0, part.getLowerLeft().getY(), 1.9e-16);
		assertEquals(10.0, part.getUpperRight().getX(), 1.9e-16);
		assertEquals(10.0, part.getUpperRight().getY(), 1.9e-16);

	}

	@Test
	public void testAddLine() {
		GridPart part = new GridPart(0, 0, 10, 10);
		Line line = new Line(0.0, 0.0, 10.0, 10.0);
		part.addLine(line);
		if (part.getLines().size() == 1) {

		} else
			fail("There are " + part.getLines().size() + " lines in the list");
	}

	@Test
	public void testIsPointInGridPart() {
		GridPart part = new GridPart(0.0, 0.0, 10.0, 10.0);
		Point test = new Point(1.0, 1.0);
		assertTrue(part.isPointInGridPart(test));
		test = new Point(0.0, 0.0);
		assertTrue(part.isPointInGridPart(test));
		test = new Point(0.0, 10.0);
		assertFalse(part.isPointInGridPart(test));
		test = new Point(10.0, 10.0);
		assertFalse(part.isPointInGridPart(test));
		test = new Point(10.0, 0.0);
		assertFalse(part.isPointInGridPart(test));
		test = new Point(0.0, 5.0);
		assertTrue(part.isPointInGridPart(test));
		test = new Point(5.0, 10.0);
		assertFalse(part.isPointInGridPart(test));
		test = new Point(-5.0, -5.0);
		assertFalse(part.isPointInGridPart(test));
		test = new Point(-5.0, 15.0);
		assertFalse(part.isPointInGridPart(test));

		

	}

	@Test
	public void testFindIntersections() {
		GridPart part = new GridPart(0, 0, 10, 10);
		Line line = new Line(0, 0, 15, 15);
		Line intersect = new Line(0, 10, 10, 0);
		Line nointersect = new Line(4, 8, 13, 14);
		part.addLine(line);
		part.addLine(intersect);
		part.addLine(nointersect);
		ArrayList<Point> result = (ArrayList<Point>) part.findIntersections();

		Point tmp = result.get(0);
		assertEquals(5.0, tmp.getX(), 1.9e-16);
		assertEquals(5.0, tmp.getY(), 1.9e-16);
		assertEquals(1,result.size());
		part.addLine(new Line(2,10,2,0));
		result = (ArrayList<Point>) part.findIntersections();
	assertEquals(3,result.size())	;
	}

	@Test
	public void testToString() {
		GridPart part = new GridPart(0, 0, 10, 10);
		part.addLine(new Line(0, 0, 1, 1));
		String sth = "from (0.0 , 0.0) to (10.0 , 10.0)\nLines:\n(0.0 , 0.0) (1.0 , 1.0)\n";
		System.out.println(sth);
		System.out.println(part.toString());
		Assert.assertEquals(sth, part.toString());
	}

}
